package app.util;


import spark.*;
import static app.util.RequestUtil.*;

import java.util.ArrayList;
import java.util.List;
import app.book.BookCategory;

public class Filters {

    // If a user manually manipulates paths and forgets to add
    // a trailing slash, redirect the user to the correct path
    public static Filter addTrailingSlashes = (Request request, Response response) -> {
        if (!request.pathInfo().endsWith("/")) {
            response.redirect(request.pathInfo() + "/");
        }
    };

    // Locale change can be initiated from any page
    // The locale is extracted from the request and saved to the user's session
    public static Filter handleLocaleChange = (Request request, Response response) -> {
        if (getQueryLocale(request) != null) {
            request.session().attribute("locale", getQueryLocale(request));
            response.redirect(request.pathInfo());
        }
    };

    // Enable GZIP for all responses
    public static Filter addGzipHeader = (Request request, Response response) -> {
        response.header("Content-Encoding", "gzip");
    };
    
    public static List<Property> getBookCategoryOptions() {
    	List<Property> cats = new ArrayList<>();
    	for(BookCategory cat: BookCategory.values()) {
    	    String name = "";
            switch(cat) {
                case Chinese -> {
                    name = "中文";
                }
                case English -> {
                    name = "英文";
                }
                case Poem -> {
                    name = "诗词";
                }
            }
    		Property p = new Property(cat.name(), name);
    		cats.add(p);
    	}
    	return cats;
    }
}
